package de.wbh.gwt.form.layouter.gwt.ui.view;

import javax.inject.Inject;

import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

import de.wbh.gwt.form.layouter.gwt.ui.view.LoadFieldsViewImpl.Resources.Style;
import de.wbh.gwt.form.layouter.gwt.ui.view.widget.Button;
import de.wbh.gwt.form.layouter.shared.activity.LoadFieldsView;

public class LoadFieldsViewImpl extends Composite implements LoadFieldsView {

	interface LoadFieldsViewImplUiBinder extends UiBinder<Widget, LoadFieldsViewImpl> {
	}

	public static interface Resources extends ClientBundle {

		static interface Style extends CssResource {
			String contentPanel();

			String headline();

			String panel();
		}

		@Source("loadFieldsViewImpl.css")
		Style style();

	}

	@UiField
	Button loadSelectedButton;

	@UiField
	FormPanel form;

	@UiField
	TextBox loadFromUrlTextBox;

	private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL() + "upload";

	private static LoadFieldsViewImplUiBinder uiBinder = GWT.create(LoadFieldsViewImplUiBinder.class);

	@UiField(provided = true)
	Resources resources;

	@UiField(provided = true)
	CellList<String> serverList = new CellList<String>(new TextCell());

	private LoadFieldsView.Presenter presenter;

	private final SingleSelectionModel<String> singleSelectionModel = new SingleSelectionModel<String>();

	private final Style style;
	@UiField
	FileUpload localFileUpload;

	@UiField
	Button loadLocalButton;

	private String currentLocalName;

	@Inject
	public LoadFieldsViewImpl(Resources resources) {
		this.resources = resources;
		this.style = resources.style();
		this.style.ensureInjected();
		initWidget(uiBinder.createAndBindUi(this));
		this.localFileUpload.setName("uploadFormElement");
		this.localFileUpload.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				FileUpload upload = (FileUpload) event.getSource();
				if ((upload.getFilename().endsWith("efd") || upload.getFilename().endsWith("EFD"))) {
					LoadFieldsViewImpl.this.loadLocalButton.setEnabled(true);
				} else {
					LoadFieldsViewImpl.this.loadLocalButton.setEnabled(false);
					Window.alert("Bitte wählen Sie ein efd-Datei aus.");
				}
			}
		});

		this.form.setAction(UPLOAD_ACTION_URL);
		this.form.setEncoding(FormPanel.ENCODING_MULTIPART);
		this.form.setMethod(FormPanel.METHOD_POST);

		this.serverList.setSelectionModel(this.singleSelectionModel);
		this.singleSelectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
			@Override
			public void onSelectionChange(SelectionChangeEvent event) {
				if (LoadFieldsViewImpl.this.singleSelectionModel.getSelectedObject() != null) {
					LoadFieldsViewImpl.this.loadSelectedButton.setEnabled(true);
				}

			}
		});

		this.form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				if (event.getResults().contains("Die Datei exsistiert schon.")) {
					Window.alert("Die Datei ist schon auf dem Server vorhanden.");
				} else {
					LoadFieldsViewImpl.this.presenter.loadLocal(LoadFieldsViewImpl.this.currentLocalName);
				}
			}
		});

	}

	@Override
	public CellList<String> getServerList() {
		return this.serverList;
	}

	@UiHandler("loadFromUrlButton")
	public void onLoadFromUrlButtonClicked(ClickEvent event) {
		if (this.presenter != null) {
			// nur wenn Text eingegeben ist
			if (!this.loadFromUrlTextBox.getText().isEmpty() || (!this.loadFromUrlTextBox.getText().endsWith("efd") || this.loadFromUrlTextBox.getText().endsWith("EFD"))) {
				this.presenter.loadFromUrl(this.loadFromUrlTextBox.getText());
			} else {
				Window.alert("Bitte geben sie eine URL ein unter der eine Efd-Datei verfügbar ist.");
			}
		}
	}

	@UiHandler("loadLocalButton")
	public void onLoadLocalButtonClicked(ClickEvent event) {
		this.currentLocalName = this.localFileUpload.getFilename();
		this.form.submit();
	}

	@UiHandler("loadSelectedButton")
	public void onLoadSelectedButtonClicked(ClickEvent event) {
		if (this.presenter != null) {
			this.presenter.loadFromServer(this.singleSelectionModel.getSelectedObject());
		}
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	};
}
